﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.Inventory;
using S4Store.Service;
using S4Store.Models;

namespace S4Store.Controllers
{
    public class InventoryController : ReportBaseController
    {

        public CarKpiService CarKpiService { get; set; }
        public ReportService ReportService { get; set; }
        public CarService CarService { get; set; }

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            using(var t=new Transtion()){
                t.Inject(CarKpiService);
                t.Inject(ReportService);
                var brands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var report = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                }
                var monthreport = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                model.Total = monthreport.Inventory;
                model.TotalXs = monthreport.InventoryCoefficient;
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "Inventory";
            model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='库存结构历史数据查询' href='" + Url.Action("History") + "' ><span>历史数据查询</span></a>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult History()
        {
            HistoryModel model = new HistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }

            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                t.Inject(ReportService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ReportService.GetSaleReport(dt.Year, dt.Month, Store.Id);                    
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "History";
            model.BarModel.Controller = "Inventory";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }

        public ActionResult CarBrandInventory()
        {
            CarBrandInventoryModel model = new CarBrandInventoryModel();
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                t.Inject(ReportService);
                t.Inject(CarService);
                var brand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId);
                model.CarBrand = brand;
                var carmodels = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value).ToList();
                foreach (var m in carmodels)
                {
                    var report = ReportService.GetCarModelSaleReport(Month.Year, Month.Month, Store.Id, m.Id);
                    model.Reports.Add(report);
                }
                var monthreport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id,SearchCarBrandId.Value);
                model.Total = monthreport.Inventory;
                model.TotalXs = monthreport.InventoryCoefficient;
                model.BrandReport = monthreport;
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarBrandInventory";
            model.BarModel.Controller = "Inventory";
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SaleTutors ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<ul class='toolBar' style='width:300px'>"+
                                       "<li><a  class='historyrecord edit' backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.Sale.ToInt() ,referdesc="销售管理>新车库存结构动态>车型查询" } ) + "' target='dialog' width='30%'><span class='icon edit'>填写改善指导建议</span></a></li>" +
                                       "<li><a  class='historyrecord edit'  backtype='html' href='" + Url.Action("CarBrandHistory", new { SearchCarBrandId = SearchCarBrandId.Value }) + "'  target='navtab' title='" + model.CarBrand.Name + "库存结构历史数据' ><span class='icon search'>库存结构历史数据查询</span></a></li>" +
                                       "</ul>";
									   }else
		{
			 model.BarModel.PlusItem = "<ul class='toolBar' style='width:300px'>"+                                      
                                       "<li><a  class='historyrecord edit'  backtype='html' href='" + Url.Action("CarBrandHistory", new { SearchCarBrandId = SearchCarBrandId.Value }) + "'  target='navtab' title='" + model.CarBrand.Name + "库存结构历史数据' ><span class='icon search'>库存结构历史数据查询</span></a></li>" +
                                       "</ul>";
			}
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        public ActionResult CarBrandHistory()
        {
            CarBrandHistoryModel model = new CarBrandHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }

            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                t.Inject(ReportService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ReportService.GetCarBrandSaleReport(dt.Year, dt.Month, Store.Id, SearchCarBrandId.Value);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarBrandHistory";
            model.BarModel.Controller = "Inventory";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }


        public ActionResult AgedIndex()
        {
            AgedIndexModel model = new AgedIndexModel();
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                t.Inject(ReportService);
                var carmodels = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var carmodel in carmodels)
                {
                    var report = ReportService.GetCarModelSaleReport(Month.Year, Month.Month, Store.Id, carmodel.Id);
                    if (report.CKC > 0)
                    {
                        model.Reports.Add(report);
                    }
                }
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "AgedIndex";
            model.BarModel.Controller = "Inventory";
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SaleTutors ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.Sale.ToInt() ,referdesc="销售管理>新车库存结构动态>高龄库存" }) + "' target='dialog' width='30%'><span class='icon edit'>填写改善指导建议</span></a></li></ul>";
		}
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }
    }
}
